package com.ruoyi.cat.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.ruoyi.cat.domain.CatArticletaskTable;
import com.ruoyi.cat.domain.CatShareuserTable;
import com.ruoyi.cat.domain.DTO.MyHomeDTO;
import com.ruoyi.cat.domain.DTO.MyShareUserDTO;
import com.ruoyi.cat.domain.DTO.TradeNumDTO;
import com.ruoyi.cat.domain.DTO.UserActivityDTO;
import com.ruoyi.cat.domain.VO.DataScreeningVO;
import com.ruoyi.cat.domain.VO.MyShareVO;
import com.ruoyi.cat.domain.VO.TradeNumVO;
import com.ruoyi.cat.mapper.CatShareuserTableMapper;
import com.ruoyi.cat.service.*;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 分享用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-24
 */
@Service
public class CatShareuserTableServiceImpl implements ICatShareuserTableService
{
    @Autowired
    private CatShareuserTableMapper catShareuserTableMapper;

    @Autowired
    private ICatIprecordTableService catIprecordTableService;

    @Autowired
    private ICatChanceService iCatChanceService;

    @Autowired
    private ICatArticletaskTableService catArticletaskTableService;

    /**
     * 查询分享用户
     * 
     * @param id 分享用户主键
     * @return 分享用户
     */
    @Override
    public CatShareuserTable selectCatShareuserTableById(Long id)
    {
        return catShareuserTableMapper.selectCatShareuserTableById(id);
    }

    /**
     * 查询分享用户列表
     * 
     * @param catShareuserTable 分享用户
     * @return 分享用户
     */
    @Override
    public List<CatShareuserTable> selectCatShareuserTableList(CatShareuserTable catShareuserTable)
    {
        return catShareuserTableMapper.selectCatShareuserTableList(catShareuserTable);
    }

    /**
     * 新增分享用户
     * 
     * @param catShareuserTable 分享用户
     * @return 结果
     */
    @Override
    public int insertCatShareuserTable(CatShareuserTable catShareuserTable)
    {
        catShareuserTable.setCreateTime(DateUtils.getNowDate());
        return catShareuserTableMapper.insertCatShareuserTable(catShareuserTable);
    }

    /**
     * 修改分享用户
     * 
     * @param catShareuserTable 分享用户
     * @return 结果
     */
    @Override
    public int updateCatShareuserTable(CatShareuserTable catShareuserTable)
    {
        return catShareuserTableMapper.updateCatShareuserTable(catShareuserTable);
    }

    /**
     * 批量删除分享用户
     * 
     * @param ids 需要删除的分享用户主键
     * @return 结果
     */
    @Override
    public int deleteCatShareuserTableByIds(Long[] ids)
    {
        return catShareuserTableMapper.deleteCatShareuserTableByIds(ids);
    }

    /**
     * 删除分享用户信息
     * 
     * @param id 分享用户主键
     * @return 结果
     */
    @Override
    public int deleteCatShareuserTableById(Long id)
    {
        return catShareuserTableMapper.deleteCatShareuserTableById(id);
    }

    @Override
    public List<MyShareUserDTO> getMyShareUser(MyShareVO myShareVO) {
        List<MyShareUserDTO> list = catShareuserTableMapper.getMyShareUserTask(myShareVO);
        list.stream().forEach(e->{
            //当阅读量大于等于目标阅读量时，文章审核状态为3 结束活动
            if (e.getType().equals("1")) {
                if (null != e.getTargetNum() && e.getTargetNum() > 0) {
                    if(e.getQuantityData() >= e.getTargetNum()){
                        CatShareuserTable catShareuserTable = new CatShareuserTable();
                        catShareuserTable.setId(e.getId());
                        e.setArticleExamine("3");
                        catShareuserTable.setArticleExamine("3");
                        catShareuserTable.setQuantity(e.getTargetNum());
                        CatArticletaskTable catArticletaskTable = new CatArticletaskTable();
                        catArticletaskTable.setId(e.getArticleId());
                        catArticletaskTable.setArticleExamine("3");
                        catShareuserTableMapper.updateCatShareuserTable(catShareuserTable);
                        catArticletaskTableService.updateAdvertTime(catArticletaskTable);
                    }
                }

                if (e.getEndTime().compareTo(DateUtils.getNowDate()) <= 0) {
                    if (!e.getArticleExamine().equals("3")){
                        CatShareuserTable catShareuserTable = new CatShareuserTable();
                        catShareuserTable.setId(e.getId());
                        e.setArticleExamine("3");
                        catShareuserTable.setArticleExamine("3");
                        catShareuserTableMapper.updateCatShareuserTable(catShareuserTable);
                    }
                }
                DataScreeningVO dataquery = new DataScreeningVO();
                dataquery.setType("1");
                dataquery.setUserId(myShareVO.getUserId());
                dataquery.setActivityId(e.getArticleId());
                e.setMyVisitors(catIprecordTableService.getIprecordTableBy(dataquery));
                TradeNumVO tradeNumDTO  = new TradeNumVO();
                tradeNumDTO.setUserId(myShareVO.getUserId());
                tradeNumDTO.setType("1");
                tradeNumDTO.setTradeType("0");
                tradeNumDTO.setActivityId(e.getArticleId());
                TradeNumDTO tradeNumDTO1 = catIprecordTableService.getTradeNum(tradeNumDTO);
                if (tradeNumDTO1 != null) {
                    if (tradeNumDTO1.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                        e.setConversionIncome(tradeNumDTO1.getTradeNum());
                    }else{
                        e.setConversionIncome(BigDecimal.ZERO);
                    }
                }
                dataquery.setUserId(myShareVO.getUserId());
                e.setQuantity(catIprecordTableService.getIprecordTableBy(dataquery));
            }else{
                DataScreeningVO dataquery = new DataScreeningVO();
                dataquery.setType("2");
                dataquery.setUserId(myShareVO.getUserId());
                dataquery.setActivityId(e.getId());
                e.setMyVisitors(catIprecordTableService.getIprecordTableBy(dataquery));
                TradeNumVO tradeNumDTO  = new TradeNumVO();
                tradeNumDTO.setUserId(myShareVO.getUserId());
                tradeNumDTO.setType("2");
                tradeNumDTO.setTradeType("0");
                Long tradeNum =  iCatChanceService.getChanceDataByUserIdAndUserId(e.getArticleId(), myShareVO.getUserId());
                if (null != e.getActivityBaseCost() && e.getActivityBaseCost().compareTo(BigDecimal.ZERO) != 0) {
                    e.setConversionIncome(e.getActivityBaseCost().multiply(new BigDecimal(tradeNum)));
                }else{
                    e.setConversionIncome(BigDecimal.ZERO);
                }
                Long data =  iCatChanceService.getChanceDataByUserId(e.getArticleId(), e.getUserId());
                e.setParticipants(data);

                System.out.println(e.getParticipants());

                /*TradeNumDTO tradeNumDTO1 = catIprecordTableService.getTradeNum(tradeNumDTO);
                if (tradeNumDTO1 != null) {
                    if (tradeNumDTO1.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                        e.setConversionIncome(tradeNumDTO1.getTradeNum());
                    }else{
                        e.setConversionIncome(BigDecimal.ZERO);
                    }
                }*/
            }

        });
        return list;
    }

    @Override
    public List<UserActivityDTO> getMyShareUserActivity(MyShareVO myShareVO) {
        return catShareuserTableMapper.getMyShareUserActivity(myShareVO);
    }

    @Override
    public List<MyHomeDTO> getMyHome(MyShareVO myShareVO) {

        List<MyHomeDTO> list =  catShareuserTableMapper.getMyHome(myShareVO);
        list.stream().forEach(e->{

            if (e.getType().equals("1")) {
                if (null != e.getTargetNum() && e.getTargetNum() > 0){
                    if (e.getQuantity() >= e.getTargetNum()) {
                        CatShareuserTable catShareuserTable = new CatShareuserTable();
                        catShareuserTable.setId(e.getId());
                        e.setArticleExamine("3");
                        catShareuserTable.setArticleExamine("3");
                        catShareuserTable.setQuantity(e.getTargetNum());
                        CatArticletaskTable catArticletaskTable = new CatArticletaskTable();
                        catArticletaskTable.setId(e.getId());
                        catArticletaskTable.setArticleExamine("3");
                        //   catShareuserTableMapper.updateCatShareuserTable(catShareuserTable);
                        catArticletaskTableService.updateAdvertTime(catArticletaskTable);
                    }
                }


                if (e.getEndTime().compareTo(DateUtils.getNowDate()) <= 0) {
                    if (!e.getArticleExamine().equals("3")){
                        CatShareuserTable catShareuserTable = new CatShareuserTable();
                        catShareuserTable.setId(e.getId());
                        e.setArticleExamine("3");
                        catShareuserTable.setArticleExamine("3");
                        catShareuserTableMapper.updateCatShareuserTable(catShareuserTable);
                    }
                }
            }
         //   if (e.getType().equals("1")) {
           e.setParticipants(iCatChanceService.getChanceDataByUserId(e.getId(), null));
           DataScreeningVO dataquery = new DataScreeningVO();
            dataquery.setType(e.getType());
            dataquery.setActivityId(e.getId());
            e.setQuantity(catIprecordTableService.getIprecordTableBy(dataquery));

            TradeNumVO tradeNumDTO  = new TradeNumVO();
            tradeNumDTO.setUserId(myShareVO.getUserId());
            tradeNumDTO.setType(e.getType());
            tradeNumDTO.setTradeType("0");
            tradeNumDTO.setActivityId(e.getId());
            TradeNumDTO tradeNumDTO1 = catIprecordTableService.getTradeNum(tradeNumDTO);
            if (tradeNumDTO1 != null) {
                if (tradeNumDTO1.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                    e.setConversionIncome(tradeNumDTO1.getTradeNum());
                }else{
                    e.setConversionIncome(BigDecimal.ZERO);
                }
            }else{
                e.setConversionIncome(BigDecimal.ZERO);
            }
         //   }
        });
        return list;
    }

    @Override
    public CatShareuserTable getShareUser(CatShareuserTable catShareuserTable) {
        return catShareuserTableMapper.getShareUser(catShareuserTable);
    }
}
